<template>
  <div>
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate" v-auth="'ACTION_addCommpany'"> 新增企业</a-button>
        <a-button type="primary" @click="handleCompanyGoods(selectedRow)" :disabled="!hasSelectedRows" v-auth="'ACTION_commpanyGoodsSet'"> 产品设置 </a-button>
        <a-button type="primary" @click="handleCompanyBalanceLog(selectedRow)" :disabled="!hasSelectedRows"> 余额明细 </a-button>
        <a-button type="primary" @click="handleCompanyUser(selectedRow)" :disabled="!hasSelectedRows"> 前端账号管理 </a-button>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template>
    </BasicTable>
    <CompanyDrawer @register="registerDrawer" @success="reload" />
    <CompanyGoodsDrawer @register="registerCompanyGoodsDrawer" />
    <CompanyBalanceLogModal @register="registerCompanyBalanceLogModal" />
    <CompanyBalanceRechargeModal @register="registerCompanyBalanceRechargeModal" @success="handleSuccess" />
    <CompanyUserIndexDrawer @register="registerCompanyUserIndexDrawer" />
  </div>
</template>
<script lang="ts" setup name="commpany">
import { ActionItem, BasicTable, TableAction } from '@apps/antd-admin/src/components/Table';
import { useDrawer } from '@apps/antd-admin/src/components/drawer';
import { useModal } from '@apps/antd-admin/src/components/modal';
import CompanyDrawer from './components/CompanyDrawer.vue';
import CompanyGoodsDrawer from './components/CompanyGoodsDrawer.vue';
import CompanyBalanceLogModal from './components/CompanyBalanceLogModal.vue';
import CompanyBalanceRechargeModal from './components/CompanyBalanceRechargeModal.vue';
import CompanyUserIndexDrawer from './components/CompanyUserIndexDrawer.vue';
import { useBaseTable } from '@apps/antd-admin/src/hooks';

import { columns, searchFormSchema } from './data/data';
import { Api } from './data/api';

const [registerDrawer, { openDrawer }] = useDrawer();
const [registerCompanyGoodsDrawer, { openDrawer: openCompanyGoodsDrawer }] = useDrawer();
const [registerCompanyUserIndexDrawer, { openDrawer: openCompanyUserIndexDrawer }] = useDrawer();
const [registerCompanyBalanceLogModal, { openModal: openCompanyBalanceLogModal }] = useModal();
const [registerCompanyBalanceRechargeModal, { openModal: openCompanyBalanceRechargeModal }] = useModal();
const { prefixCls, tableContext, handleDelete, handleSuccess, handlePost, handleBatchDelete } = useBaseTable({
  designScope: 'company-template',
  tableProps: {
    //表格标题
    title: '合作企业列表',
    //主键字段
    rowKey: 'companyId',
    //列表接口
    listUrl: Api.page,
    //删除接口
    delUrl: Api.remove,
    //列定义
    columns,
    //查询表单配置
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
    },
    //操作列配置
    actionColumn: {
      width: 120,
      title: '操作',
      dataIndex: 'action',
      slots: { customRender: 'action' },
      fixed: 'right',
    },
    clickToRowSelect: true,
    rowSelection: {
      type: 'radio',
    },
  },
});

const [registerTable, { reload }, { rowSelection, selectedRow, hasSelectedRows }] = tableContext;

/**
 * 行内操作栏
 */
function getTableAction(record): ActionItem[] {
  return [
    {
      label: '编辑',
      onClick: handleEdit.bind(null, record),
      auth: 'ACTION_editCommpany',
    },
    {
      label: '余额充值',
      onClick: handleRecharge.bind(null, record),
      auth: 'ACTION_rechargeAmount',
    },
  ];
}

function handleCreate() {
  openDrawer(true, {
    isUpdate: false,
  });
}

function handleEdit(record: Recordable) {
  openDrawer(true, {
    record,
    isUpdate: true,
  });
}

function handleCompanyGoods(record) {
  openCompanyGoodsDrawer(true, {
    record,
    isUpdate: false,
  });
}

function handleCompanyBalanceLog(record) {
  openCompanyBalanceLogModal(true, {
    record,
    isUpdate: false,
  });
}

function handleRecharge(record) {
  openCompanyBalanceRechargeModal(true, {
    record,
    isUpdate: true,
  });
}

function handleCompanyUser(record) {
  openCompanyUserIndexDrawer(true, {
    record,
    isUpdate: false,
  });
}
</script>
